﻿import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.enumType.MmochatServerType;
import com.dragon.mmochat.service.MmochatGmService;

/*
 1、将资源文件名都改成英文名
 2、将资源文件放到resource目录里
 ClassLoader loader = Thread.currentThread().getContextClassLoader();
 URL url = loader.getResource("base/map.xml");
 File mapFile = new File(url.getFile());

 从配置文件读取启动参数
 * */
public class StartServlet implements javax.servlet.Servlet {

	/*
	 * public StartServlet() throws Exception {
	 * System.out.println("StartServlet"); InputStream in =
	 * StartServlet.class.getClassLoader()
	 * .getResourceAsStream("server.properties"); Properties p = new
	 * Properties(); p.load(in);
	 * 
	 * String ModuleIDStr = p.getProperty("ModuleID"); String ModuleInstIDStr =
	 * p.getProperty("ModuleInstID"); System.out.println("ModuleID=" +
	 * ModuleIDStr + ",ModuleInstIDStr=" + ModuleInstIDStr);
	 * MmochatMain.main(new String[] { "ModuleID=" + ModuleIDStr,
	 * "ModuleInstID=" + ModuleInstIDStr }); }
	 */
	@Override
	public void destroy() {
		if (!MmochatGmService.isSystemExit()) {
			System.out.println("StartServlet destroy called!");
			if (MmochatMain.getModuleInstID() != MmochatConstant.serverListModuleId) {
				MmochatGmService.exitServer("本区维护中!");
			}
		} else {
			System.out.println("exitServer call StartServlet destroy!");
		}
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException {
		try {
			System.out.println("StartServlet");
			InputStream in = StartServlet.class.getClassLoader()
					.getResourceAsStream("server.properties");
			Properties p = new Properties();
			p.load(in);

			String ModuleIDStr = p.getProperty("ModuleID");
			String ModuleInstIDStr = p.getProperty("ModuleInstID");
			String ServerTypeStr = p.getProperty("ServerType");
			String TargetPkServerInstIDStr = p
					.getProperty("TargetPkServerInstID");
			if (ServerTypeStr == null) {
				ServerTypeStr = "gameServer";
			}
			if (TargetPkServerInstIDStr == null) {
				TargetPkServerInstIDStr = "0x0";
			}

			System.out.println("ModuleID=" + ModuleIDStr + ",ModuleInstIDStr="
					+ ModuleInstIDStr + ",ServerType=" + ServerTypeStr
					+ ",TargetPkServerInstID=" + TargetPkServerInstIDStr);

			MmochatMain.main(new String[] { "ModuleID=" + ModuleIDStr,
					"ModuleInstID=" + ModuleInstIDStr,
					"ServerType=" + ServerTypeStr,
					"TargetPkServerInstID=" + TargetPkServerInstIDStr });
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}
}
